package com.gxgeek.springweb.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.nio.charset.StandardCharsets;

/**
 * @author gaoxing
 * @create 2018-08-30 下午2:05
 */
@Slf4j
public class WebUtils {
    public static void writeResponse(HttpServletResponseWrapper rsp, int httpStatus, String content) {
        try {
            HttpServletResponse response = (HttpServletResponse)rsp.getResponse();
            response.reset();//    // 重置response对象中的缓冲区，该方法可以不写，但是你要保证response缓冲区没有其他数据，否则导出可能会出现问题，建议加上
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            response.setContentType(MediaType.APPLICATION_JSON_UTF8.toString());
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setStatus(httpStatus);
            response.getOutputStream().write(content.getBytes(StandardCharsets.UTF_8));
        } catch (Exception var4) {
            log.error("往输出流中输出内容[" + content + "]异常", var4);
        }

    }

}
